<template>
	<!-- 招标需求业务请示 -->
	<CommonTitle titleName="招标需求业务请示"></CommonTitle>
	<div class="box">
		<el-form
			:inline="true"
			label-width="140"
			:model="formInline"
			:rules="rules"
			ref="formRef"
			class="demo-form-inline"
		>
			<el-row>
				<el-col :span="8" v-if="state.disabled">
					<el-form-item label="审核编号">
						<el-input v-model="formInline.flowId" disabled placeholder="" />
					</el-form-item>
				</el-col>
				<el-col :span="8" v-if="state.disabled">
					<el-form-item label="审核状态">
						<el-select v-model="formInline.examineStatus" disabled>
							<el-option
								v-for="item in state.auditStatus"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8" v-if="state.disabled">
					<el-form-item label=" ">
						<el-button type="primary" @click="openOa" class="button_edit">查看流程审批</el-button>
					</el-form-item>
				</el-col>
				<el-col :span="8"
					><el-form-item label="所属部门">
						<el-input
							v-model="formInline.departmentName"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="所属项目集">
						<el-input
							v-model="formInline.projectSetName"
							disabled
							placeholder=""
						/>

						<!-- <el-select
              v-model="formInline.projectSetName"
              disabled
              placeholder="Activity zone"
            >
              <el-option label="Zone one" value="shanghai" />
              <el-option label="Zone two" value="beijing" />
            </el-select> -->
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目集编号">
						<el-input
							v-model="formInline.projectSetId"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目负责人">
						<el-input
							v-model="formInline.responsibleUserName"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="所属项目">
						<el-input
							v-model="formInline.projectName"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目编号">
						<el-input
							v-model="formInline.projectId"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8" v-if="!state.disabled">
					<el-form-item label="业务描述">
						<el-input
							v-model="formInline.problemNature"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="所属阶段">
						<el-input
							v-model="formInline.projectStage"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="项目类型">
						<el-input
							v-model="formInline.projectType"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="所属任务">
						<el-input
							v-model="formInline.projectTask"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="任务编号">
						<el-input
							v-model="formInline.projectTaskCode"
							disabled
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="业务标题" prop="requestTitle">
						<el-input
							:disabled="state.disabled"
							v-model="formInline.requestTitle"
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<!-- <el-col :span="8">
          <el-form-item label="紧急程度" prop="urgentDegree">
            <el-select
              :disabled="state.disabled"
              v-model="formInline.urgentDegree"
              placeholder=""
            >
              <el-option
                v-for="item in urgencyList"
                :key="item"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col> -->
				<el-col :span="8">
					<el-form-item label="预估金额" prop="estimateMoney">
						<el-input
							:disabled="state.disabled"
							v-model="formInline.estimateMoney"
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="业务类别" prop="businessType">
						<el-select
							:disabled="state.disabled"
							v-model="formInline.businessType"
							placeholder=""
						>
							<el-option
								v-for="item in businessTypeList"
								:key="item"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<!-- <el-col :span="8">

           <el-form-item label="服务范围" prop="startCheckTime">
            <el-date-picker
              v-model="formInline.startCheckTime"
              :disabled="state.disabled"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
              :size="size"
            />
          </el-form-item>
        </el-col> -->
				<el-col :span="8">
					<el-form-item label="项目业务类型" prop="requestType">
						<el-select
							:disabled="state.disabled"
							v-model="formInline.requestType"
							placeholder=""
						>
							<el-option
								v-for="item in projectList"
								:key="item"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8" v-if="!state.disabled">
					<el-form-item label="招标方式" prop="projectTendersWay">
						<el-select
							:disabled="state.disabled"
							v-model="formInline.projectTendersWay"
							placeholder=""
							clearable
							filterable
						>
							<el-option
								v-for="item in state.projectTendersWayList"
								:key="item.value"
								:label="item.label"
								:value="item.value"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="会审部门" prop="reviewDepartment">
						<el-select
							v-model="formInline.reviewDepartment"
							value-key=""
							:disabled="state.disabled"
							placeholder="会审部门"
							clearable
							multiple
							filterable
						>
							<el-option
								v-for="item in state.departmentOptions"
								:key="item.departmentId"
								:label="item.departmentName"
								:value="item.departmentId"
							>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="业务简述" prop="projectBiddingExplain">
						<el-input
							:disabled="state.disabled"
							v-model="formInline.projectBiddingExplain"
							type="textarea"
							placeholder=""
						/>
					</el-form-item>
				</el-col>

				<el-col :span="24">
					<el-form-item label="相关附件" prop="files">
						<FileMinioUpload
							:disabled="state.disabled"
							:restriction="true"
							:restrictionLimit="9"
							@updateFiles="updateFiles"
							v-model:files="formInline.files"
              :show-button="!state.disabled"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<CommonTitle titleName="其他信息"></CommonTitle>
				</el-col>
				<el-col :span="8">
					<el-form-item label="申请人">
						<el-input disabled v-model="formInline.createName" placeholder="" />
					</el-form-item>
				</el-col>

				<el-col :span="8">
					<el-form-item label="申请部门">
						<el-input
							disabled
							v-model="formInline.createDepartmentName"
							placeholder=""
						/>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="创建时间">
						<el-input disabled v-model="formInline.createTime" placeholder="" />
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="!state.disabled">
					<el-button @click="submit" type="primary" class="button_view">发起审批</el-button>
					<el-button type="primary" @click="goback" class="button_cancel2"> 取消 </el-button>
        </el-col>
			</el-row>
		</el-form>
	</div>
</template>

<script setup>
import { businessApi, department } from "@/api/project";
import {ref, reactive, onBeforeMount, onMounted, watch, nextTick} from "vue";
import {
	ElForm,
	ElFormItem,
	ElRow,
	ElCol,
	ElMessage,
	ElSelect,
	ElOption
} from "element-plus";
import CommonTitle from "@/components/CommonTitle.vue";
import FileMinioUpload from "@/components/FileMinioUpload.vue";
import { useRouter, useRoute } from "vue-router";
import { getURLhandler } from "@/views/constructionProjectManage/static/utools.js";
import {useSetFormData} from "@/hooks/setFormData";


let router = useRouter();
let route = useRoute();
const formInline = reactive({});

const {
	setData,
	getData,
	getAttributeNum,
	filterEmptyAttributes,
} = useSetFormData({
	key:`approve-${getURLhandler(route.path).projectId}-${getURLhandler(route.path).projectTaskId}-${getURLhandler(route.path).itemId}`,
});

// 1:待审核 2:审核成功 3:审核驳回
const state = reactive({
	auditStatus: [
		{
			label: "待审核",
			value: "1",
		},
		{
			label: "审核成功",
			value: "2",
		},
		{
			label: "审核驳回",
			value: "3",
		},
	],
	// "1公开招标",
	// "2公开竞争性谈判",
	// "3公开竞争性磋商",
	// "4择优比选",
	// "5单一来源采购（公开）"
	// "6邀请竞争性谈判", "
	// "7邀请竞争性磋商", "
	// "8择优询价",
	// "9单一来源采购（非公开）
	// projectTendersWayList:[
	//   {
	//     label: '公开招标',
	//     value: 1
	//   },
	//   {
	//     label: '公开竞争性谈判',
	//     value: 2
	//   },
	//   {
	//     label: '公开竞争性磋商',
	//     value: 3
	//   },
	//   {
	//     label: '择优比选',
	//     value: 4
	//   },
	//   {
	//     label: '单一来源采购（公开）',
	//     value: 5
	//   },
	//   {
	//     label: '邀请竞争性谈判',
	//     value: 6
	//   },
	//   {
	//     label: '邀请竞争性磋商',
	//     value: 7
	//   },
	//   {
	//     label: '择优询价',
	//     value: 8
	//   },
	//   {
	//     label: '单一来源采购（非公开）',
	//     value: 9
	//   }
	// ],
	projectTendersWayList: [
		/*{
			label: "简易询价",
			value: 1,
		},
		{
			label: "直接委托",
			value: 2,
		},*/
		{
			label: "邀请竞争性谈判",
			value: '6',
		},
		{
			label: "邀请竞争性磋商",
			value: '7',
		},
		{
			label: "择优询价",
			value: '8',
		},
		{
			label: "单一来源采购 (非公开)",
			value: '9',
		},
		{
			label: "简易询价",
			value: '10',
		},
		{
			label: "直接委托",
			value: '11',
		},
	],
	projectTaskId: "",
	departmentOptions: [],
	disabled: false,
	projectId: "",
	itemId: "",
	user: "",
});

const rules = {
  requestTitle: [
    { required: true, message: '业务标题不能为空', trigger: 'blur' }
  ],
  budgetControlPrice: [
    { required: true, message: '预算控制价不能为空', trigger: 'blur' }
  ],
  estimateMoney: [
    { required: true, message: '预估金额不能为空', trigger: 'blur' }
  ],
  urgentDegree: [
    { required: true, message: '紧急程度不能为空', trigger: 'blur' }
  ],

	startCheckTime: [
		{ required: true, message: "服务范围不能为空", trigger: "blur" },
	],
	requestType: [
		{ required: true, message: "业务类型不能为空", trigger: "blur" },
	],
	projectTendersWay: [
		{ required: true, message: "招标方式不能为空", trigger: "blur" },
	],
	businessType: [
		{ required: true, message: "业务类别不能为空", trigger: "blur" },
	],
	reviewDepartment: [
		{ required: true, message: "会审部门不能为空", trigger: "blur" },
	],
	projectBiddingExplain: [
		{ required: true, message: "议题简述不能为空", trigger: "blur" },
	],
	projectPeriodExplain: [
		{ required: true, message: "项目周期说明不能为空", trigger: "blur" },
	],
	projectControlExplain: [
		{ required: true, message: "招标控制价说明不能为空", trigger: "blur" },
	],
	files: [{ required: true, message: "议题附件不能为空", trigger: "blur" }],
};
const updateFiles = () => {};
const goback = () => {
	router.go(-1);
};
// (0:非业务的日常管理经费,1:咨询服务类,2:备用金借款,3:财务数据报出,4:其他及涉及业务的内容,5:养老内容)
const businessTypeList = reactive([
	{
		label: "非业务的日常管理经费",
		value: "12",
	},
	{
		label: "咨询服务类",
		value: "13",
	},
	{
		label: "备用金借款",
		value: "14",
	},
	{
		label: "财务数据报出",
		value: "15",
	},
	{
		label: "其他及涉及业务的内容",
		value: "16",
	},
	{
		label: "养老内容",
		value: "17",
	},
]);
// 0:公开招标,1:择优比选,2:公开竞争性谈判,3:单一来源采购(公开),4:择优询价,5:邀请竞争性磋商,6:邀请竞争性谈判,7:单一 来源采购(非公开)
const biddingCategoryList = reactive([
	{
		label: "工程类-施工类",
		value: "0",
	},
	{
		label: "工程类-货物采购类",
		value: "1",
	},
	{
		label: "工程类-勘察、设计、监理服务类",
		value: "2",
	},
	{
		label: "非工程类-修缮改造类",
		value: "3",
	},
	{
		label: "非工程类-设施设备系统维保及维修类",
		value: "4",
	},
	{
		label: "非工程类-物业服务类",
		value: "5",
	},
	{
		label: "非工程类-物资采购类",
		value: "6",
	},
	{
		label: "非工程类-其他服务类",
		value: "7",
	},
]);
//(0:普通,1:加急,2:平急,3:特急,4:特提)
const urgencyList = reactive([
	{
		label: "普通",
		value: "0",
	},
	{
		label: "加急",
		value: "1",
	},
	{
		label: "平急",
		value: "2",
	},
	{
		label: "特急",
		value: "3",
	},
	{
		label: "特提",
		value: "4",
	},
]);
// (-5403873840876272513=招标项目 6597315479569055834=投资项目 960330435988177277=建设项目 -2375352083915127206 其他项目
const projectList = reactive([
	{
		label: "招标项目",
		value: "-5403873840876272513",
	},
	{
		label: "投资项目",
		value: "6597315479569055834",
	},
	{
		label: "建设项目",
		value: "960330435988177277",
	},
	{
		label: "其他项目",
		value: "-2375352083915127206",
	},
]);
//获取所有部门信息
const getdepartmentList = () => {
	department.getAllDepartments().then((res) => {
		state.departmentOptions = res?.data || [];
	});
};
const formRef = ref(null);
const submit = () => {
	formRef.value.validate(async (valid, fields) => {
		if (valid) {
			if (formInline.files.length == 0) {
				ElMessage.error("请上传相关附件");
				return;
			}
			formInline.projectId = state.projectId;
			formInline.taskId = state.projectTaskId;
			formInline.itemId = state.itemId;
			let obj = JSON.parse(JSON.stringify(formInline));
			obj.startCheckTime = formInline.startCheckTime[0];
			obj.endCheckTime = formInline.startCheckTime[1];

			await businessApi.businessAdd(obj).then((res) => {
				if (res.code == "200") {
					ElMessage.success("操作成功");
					router.go(-1);
				}
			});
		}
	});
};
onBeforeMount(() => {
	state.projectTaskId = getURLhandler(route.path).projectTaskId;
	state.projectId = getURLhandler(route.path).projectId;
	state.itemId = getURLhandler(route.path).itemId;
	state.disabled = +getURLhandler(route.path).isLook;
});
const openOa = () => {
	window.open(formInline.routeUrl);
};

const isBool = ref(false);

watch([()=>state.disabled,()=>formInline],(newVal)=>{
	if(newVal[0]===0 &&  Object.keys(newVal[1]).length > 1 && isBool.value){
		setData(formInline);
	}
},{deep:true})


const getProjectData = () => {
	businessApi
			.businessLook({
				taskId: state.projectTaskId,
				itemId: state.itemId,
				projectId: state.projectId,
			})
			.then(async (res) => {
				if (res.code === "200") {
					let obj = {};
					if (getAttributeNum(await getData()) > getAttributeNum(res.data)) { // 缓存中的数据比接口数据的多
						obj = Object.assign({}, await getData(), filterEmptyAttributes(res.data))
					}
					if (getAttributeNum(await getData()) <= getAttributeNum(res.data)) { // 缓存中的数据比接口数据的少或者相同
						obj = filterEmptyAttributes(res.data)
					}

					for (const key in obj) {
						if (key === "startCheckTime") {
							if (state.disabled === 0) {
								formInline[key] = obj[key] || [];
							}
							if (state.disabled === 1) {
								formInline[key] = [
									obj[key] || '',
									obj['endCheckTime'] || ''
								];
							}
							continue;
						}
						if (key === "files") {
							formInline[key] = obj[key] || [];
							continue;
						}
						formInline[key] = obj[key];
					}
					await nextTick(() => {
						isBool.value = true;
					})
				}
			});
}

onMounted(() => {
	getProjectData();
	getdepartmentList();
});
</script>

<style lang="scss" scoped>
.el-form-item,
.el-select {
	width: 100%;
}
.box {
	border-radius: 10px;
	border: 1px solid #e3e7f1;
	padding: 34px 90px;
}
</style>
